package cn.jxufe.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jxufe.bean.ChatUser;
import cn.jxufe.bean.EasyUIData;
import cn.jxufe.bean.EasyUIDataPageRequest;
import cn.jxufe.bean.Message;
import cn.jxufe.entity.SeedBag;
import cn.jxufe.entity.User;
import cn.jxufe.service.ChatService;
import cn.jxufe.service.UserService;

/***
 * 管理用户信息
 * @author Banner
 *
 */
@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
    private UserService userService;
	
	/***
	 * 跳转到user/grid.jsp页面.
	 * @param session 请求
	 * @param model model
	 * @return String
	 */
	@RequestMapping("grid")
	public String grid(HttpSession session, Model model) {
		// 设置当前页面
		session.setAttribute("curPage", "userGridPage");
		return "user/grid";
	}
	
	/***
	 * 获取已排序的所有用户字段信息.
	 * @param userName 用户名
	 * @param easyUIDataPageRequest easyUI格式的数据，包含分页信息
	 * @param model model
	 * @return EasyUIData用户对象
	 */
	@RequestMapping("getList")
	@ResponseBody
	public EasyUIData<User> getList(@RequestParam(defaultValue = "doNothing") String userName, EasyUIDataPageRequest easyUIDataPageRequest, Model model) {
		System.out.println("userName = " + userName);
		
		EasyUIData<User> easyUIData = userService.getList(userName, easyUIDataPageRequest);
		System.out.println("这里的值为 = " + easyUIData.getTotal());
		return easyUIData;
	}
	
	/***
	 * 获取所有的用户信息.
	 * @param model model
	 * @return 用户信息列表
	 */
	@RequestMapping("list")
	@ResponseBody
	public List<User> list(Model model) {
		return userService.list();
	}
	
	/***
	 * 根据userId获取user，已经不再使用.
	 * @param user 用户信息
	 * @return User
	 */
	@RequestMapping(value = "getUser", produces = MediaType.APPLICATION_JSON_VALUE,
			consumes =MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public User getUser(@RequestBody User user){           
		  return userService.getUser(user);
	}
	
	/***
	 * 保存用户信息.
	 * @param session 请求
	 * @param user 用户信息
	 * @return Message
	 */
	@RequestMapping("save")
	@ResponseBody
	public Message save(HttpSession session, User user) {
		return userService.save(session, user);
	}
	
	/***
	 * 跟新用户信息.
	 * @param session 请求
	 * @param user 用户信息
	 * @return Message
	 */
	@RequestMapping(value = "updateUser")
	@ResponseBody
	public Message updateUser(HttpSession session, @RequestBody User user){
		System.out.println("user的内容 ====================== ");
		System.out.println(user.getId());
		return userService.updateUser(session, user);
	}
	
	/***
	 * 删除用户信息.
	 * @param session 请求
	 * @param user 用户信息
	 * @return Message
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Message delete(HttpSession session, User user) {
		System.out.println("正在删除数据！！！");
		return userService.delete(session, user);
	}
	
	/***
	 * 用户登录（模拟登录）.
	 * @param session 请求
	 * @param model model
	 * @return String
	 */
	@RequestMapping("userLogin")
	public String userChoice(HttpSession session, Model model) {
		// 设置当前页面
		session.setAttribute("curPage", "userLoginPage");
		return "user/userLogin";
	}
	
	/***
	 * 设置当前在线的用户
	 * @param session 请求
	 * @param user 用户信息
	 * @return Message
	 */
	@RequestMapping(value = "setCurUser")
	@ResponseBody
	public Message setCurUser(HttpSession session, @RequestBody User user){
		// 此处user只含有一个id
		return userService.setCurUser(session, user);
	}
	
	/***
	 * 跳转到user/seedBag.jsp页面.
	 * @param model model
	 * @return String
	 */
	@RequestMapping("seedBag")
	public String seedBag(Model model) {
		return "user/seedBag";
	}
}
